/*
 * Copyright (c) 2012, marco.tamburelli@gmail.com
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met: 
 * 
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer. 
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.bmi.gwt.ws.client.serialization;

import com.google.gwt.user.client.rpc.SerializationException;
import com.google.gwt.user.client.rpc.SerializationStreamFactory;
import com.google.gwt.user.client.rpc.SerializationStreamReader;
import com.google.gwt.user.client.rpc.SerializationStreamWriter;
import com.google.gwt.user.client.rpc.impl.ClientSerializationStreamReader;
import com.google.gwt.user.client.rpc.impl.ClientSerializationStreamWriter;
import com.google.gwt.user.client.rpc.impl.Serializer;

public abstract class ClientPayloadSerializer implements SerializationStreamFactory
{
	/**
	 * The module base URL as specified during construction.
	 */
	private final String moduleBaseURL;

	/**
	 * The name of the serialization policy file specified during construction.
	 */
	private final String serializationPolicyName;

	/**
	 * The {@link Serializer} instance used to serialize and deserialize
	 * instances.
	 */
	private final Serializer serializer;

	protected ClientPayloadSerializer(String moduleBaseURL, String serializationPolicyName, Serializer serializer)
	{
		this.moduleBaseURL = moduleBaseURL;

		this.serializer = serializer;
		this.serializationPolicyName = serializationPolicyName;
	}

	/**
	 * Returns a {@link SerializationStreamReader} that is ready for reading.
	 * 
	 * @param encoded string that encodes the response of an RPC request
	 * @return {@link SerializationStreamReader} that is ready for reading
	 * @throws SerializationException
	 */
	public SerializationStreamReader createStreamReader(String encoded) throws SerializationException
	{
		ClientSerializationStreamReader clientSerializationStreamReader = new ClientSerializationStreamReader(
				this.serializer);
		clientSerializationStreamReader.prepareToRead(encoded);
		return clientSerializationStreamReader;
	}

	/**
	 * Returns a {@link SerializationStreamWriter SerializationStreamWriter}
	 * that has had {@link ClientSerializationStreamWriter#prepareToWrite()}
	 * called on it and it has already had had the name of the remote service
	 * interface written as well.
	 * 
	 * @return {@link SerializationStreamWriter SerializationStreamWriter} that
	 *         has had {@link ClientSerializationStreamWriter#prepareToWrite()}
	 *         called on it and it has already had had the name of the remote
	 *         service interface written as well
	 */
	public SerializationStreamWriter createStreamWriter()
	{
		ClientSerializationStreamWriter clientSerializationStreamWriter = new ClientSerializationStreamWriter(
				this.serializer, this.moduleBaseURL, this.serializationPolicyName);
		clientSerializationStreamWriter.prepareToWrite();
		return clientSerializationStreamWriter;
	}

	public String getSerializationPolicyName()
	{
		return serializationPolicyName;
	}
}
